package popcorn.ticket.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Application Lifecycle Listener implementation class LoginListener
 *
 */
public class LoginListener implements HttpSessionListener {

    /**
     * Default constructor. 
     */
    public LoginListener() {

    }

	/**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent se)  { 
    	// 要将数据存在jsp -> application Servlet -> ServletContext
    	ServletContext application = se.getSession().getServletContext();
    	// 先判断是否存在属性
    	if(application.getAttribute("count") == null) {
    		// 没人访问过，设置默认值为1
    		application.setAttribute("count", 1);
    	}else {
    		// 有人访问过 需要转型 Integer后续要完成数学计算
    		Integer obj = (Integer) application.getAttribute("count");
    		// 将count的值设置为原来的值+1
    		application.setAttribute("count", obj+1);
    	}
    }
	/**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent se)  { 
    	// 要将数据存在jsp -> application Servlet -> ServletContext
    	ServletContext application = se.getSession().getServletContext();
    	// 先判断是否存在属性count
    	if(application.getAttribute("count") != null) {
    		// 有人访问过 需要转型 Integer后续要完成数学计算
    		Integer obj = (Integer) application.getAttribute("count");
    		// 将count的值设置为原来的值+1
    		application.setAttribute("count", obj-1);
    	}
    }
	
}
